package taskinstance;

public class PeriodTask extends Task {
  private String during;
  private int runTimes = 0;

  public PeriodTask(String sourceIp, String targetIp, String urlSource, String during,
      boolean osreEncapsulation, boolean encryption) {
    super(sourceIp, targetIp, urlSource, osreEncapsulation, encryption);
    this.during = during;
  }

  public int getDuringTime() {
    return Integer.parseInt(during);
  }

  public int getRunTimes() {
    return runTimes;
  }

  @Override
  public void run() {
    tm.lockTargetIp(targetIp);
    isPending = false;
    dtf.start();
    while (!dtf.isComplete()) {
      try {
        Thread.sleep(2000);
        if (dtf.isError()) {
          tm.releaseTargetIp(targetIp);
          // runningArrayList.remove(this);
          // failedArrayList.add(this);
          return;
        }
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    tm.releaseTargetIp(targetIp);
    // runningArrayList.remove(this);
    // completeArrayList.add(this);
    isPending = true;
    runTimes++;
  }

  @Override
  public String toString() {
    return "" + targetIp + "\t" + urlSource + "\truntimes: " + runTimes + "\tper: " + during;
  }

}
